package com.gosophia.govDoc.action.transientDoc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.govDoc.dto.TransientDocumentDTO;
import com.gosophia.govDoc.ws.TransientDocumentWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.MissionDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 公文起草：提交审批Action
 * 
 * @author zhuanhuanchao
 * @since 2011-01-25
 */
@Controller("SubmitTransientDocumentAction")
@Scope("prototype")
@SuppressWarnings("serial")
public class SubmitTransientDocumentAction extends BaseSupportAction {

    private Long jobId;
    private final Logger logger = Logger.getLogger(this.getClass());

    private TransientDocumentDTO transientDocumentDto;

    private String fromFlag;
    @Autowired
    private TransientDocumentWebService transientDocumentWebService;
    @Autowired
    private TaskManagementWebService taskManagementWebService;

    @Override
    public String _execute() throws Exception {

        // 得到当前用户
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
                .getUserInfoId();
        // 查询当前用户所起草的公文
        TransientDocumentDTO transientDocument = transientDocumentWebService
                .findTranDocById(transientDocumentDto.getTransientDocId());
        // 更新当前用户所起草的公文
        transientDocument.setApprovalPerson(transientDocumentDto
                .getApprovalPerson());
        transientDocument.setDescipt(transientDocumentDto.getDescipt());

        // 移除当前用户所起草的公文
        transientDocumentWebService.removeTransientDocument(transientDocument);

        if (jobId != null) {
            MissionDTO missionDto = taskManagementWebService
                    .findJobDetailByJobId(jobId).getMissionDTO();
            transientDocument.setMissionName(missionDto.getMissionName());
            transientDocument.setMissionEnd(missionDto.getScheduleEndDate());
            transientDocument.setMissionDescript(missionDto.getDescription());
            transientDocument.setImportantLevel(missionDto.getImportanceDTO()
                    .getDataValueId());
            fromFlag = "task";
        }
        logger.debug("输出提交的临时公文的信息：" + transientDocument.toString());
        transientDocumentWebService.submitTransientDocument(transientDocument);

        super.getRequestResult().setRsltCode("0");
        super.getRequestResult().setMessage("文档提交审批成功！");
        ActionContext.getContext().getSession().put("_lastOpResult",
                super.getRequestResult());
        if ("task".equals(fromFlag)) {
            return "TASK";
        } else {
            return SUCCESS;
        }
    }

    public Long getJobId() {
        return jobId;
    }

    public void setJobId(Long jobId) {
        this.jobId = jobId;
    }

    public TransientDocumentDTO getTransientDocumentDto() {
        return transientDocumentDto;
    }

    public void setTransientDocumentDto(
            TransientDocumentDTO transientDocumentDto) {
        this.transientDocumentDto = transientDocumentDto;
    }

    @Override
    public String getFromFlag() {
        return fromFlag;
    }

    @Override
    public void setFromFlag(String fromFlag) {
        this.fromFlag = fromFlag;
    }

}
